package com.example.yls.sound_pool;

import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnLoad;
    private Button btnPlay1;
    private Button btnPlay2;
    private Button btnPlay3;
    private Button btnPlay4;
    private Button btnPlay5;
    private int[] soundIdArr=new int[5];
    private SoundPool soundPool;  //SoundPool 音频池

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();

        //加载音频
        btnLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //初始化（音频池最多支持装载多少个音频即音频池的大小，
                // 指定声音的类型【一般指定为AudioManager.STREAM_MUSIC】，音频的质量【默认为0】）
                soundPool=new SoundPool(10, AudioManager.STREAM_MUSIC,0);
                //load（）从一个文件夹raw下装载一段音频资源，返回值为音频资源在SoundPool的ID。
                soundIdArr[0]=soundPool.load(MainActivity.this,R.raw.chimes,1);
                soundIdArr[1]=soundPool.load(MainActivity.this,R.raw.ding,1);
                soundIdArr[2]=soundPool.load(MainActivity.this,R.raw.enter,1);
                soundIdArr[3]=soundPool.load(MainActivity.this,R.raw.notify,1);
                soundIdArr[4]=soundPool.load(MainActivity.this,R.raw.ringout,1);
            }
        });

        //播放音频 多个音频可以同时播放
        btnPlay1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //play(音频，左声道，右声道，优先级，循环次数，播放速率） 根据资源ID，播放一段音频资源。
                soundPool.play(soundIdArr[0],1,1,1,0,1);
            }
        });
        btnPlay2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundPool.play(soundIdArr[1],1,1,1,0,1);
            }
        });
        btnPlay3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundPool.play(soundIdArr[2],1,1,1,0,1);
            }
        });
        btnPlay4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundPool.play(soundIdArr[3],1,1,1,0,1);
            }
        });
        btnPlay5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                soundPool.play(soundIdArr[4],1,1,1,0,1);
            }
        });
    }

    private void findViews() {
        btnLoad=findViewById(R.id.btn_load);
        btnPlay1=findViewById(R.id.btn_play_1);
        btnPlay2=findViewById(R.id.btn_play_2);
        btnPlay3=findViewById(R.id.btn_play_3);
        btnPlay4=findViewById(R.id.btn_play_4);
        btnPlay5=findViewById(R.id.btn_play_5);
    }
}
